package com.dau.service.system.dept.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.dau.common.constants.CommConstants;
import com.dau.common.page.PageQuery;
import com.dau.common.page.TableDataInfo;
import com.dau.entity.system.dept.DeptAndUser;
import com.dau.entity.system.dept.SysDept;
import com.dau.entity.system.user.SysUser;
import com.dau.mapper.mapper.dept.SysDeptMapper;
import com.dau.service.system.dept.SysDeptService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

@Service("syDeptService")
public class SysDeptServiceImpl extends ServiceImpl<SysDeptMapper, SysDept> implements SysDeptService {
    /**
     * 部门分页数据
     *
     * @param sysDept   系统部
     * @param pageQuery 分页参数
     * @return {@link TableDataInfo }<{@link SysUser }>
     */
    @Override
    public TableDataInfo<SysDept> selectDeptPage(SysDept sysDept, PageQuery pageQuery) {
        Page<SysDept> page = baseMapper.selectDeptPage(pageQuery.build(), this.buildQueryWrapper(sysDept));
        return TableDataInfo.build(page);
    }

    /**
     * 添加或更新
     *
     * @param sysDept 系统部门
     * @param id      用户id
     */
    @Override
    public void addOrUpdate(SysDept sysDept, Long id) {
        String userIdStr = String.valueOf(id);
        if (ObjectUtil.isNotNull(sysDept.getId())) {
            sysDept.setUpdateTime(new Date());
            sysDept.setUpdateUser(userIdStr);
        } else {
            sysDept.setStatus(CommConstants.ONE);
            sysDept.setDelFlag("1");
            sysDept.setCreateTime(new Date());
            sysDept.setCreateUser(userIdStr);
        }
    }

    /**
     * 绑定部门和用户
     *
     * @param deptAndUser 部门和用户
     */
    @Override
    public void bind(DeptAndUser deptAndUser) {
        baseMapper.bind(deptAndUser);
    }

    /**
     * 查询部门信息(含部门下所有用户信息)
     *
     * @param id 身份证件
     * @return {@link SysDept }
     */
    @Override
    public SysDept queryDeptInfo(String id) {
        SysDept sysDept = baseMapper.selectById(id);
        if (ObjectUtil.isNotNull(sysDept)) {
            List<SysUser> userList = baseMapper.queryUserListByUserId(id);
            sysDept.setUserList(userList);
        }
        return sysDept;
    }

    private Wrapper<SysDept> buildQueryWrapper(SysDept dept) {
        Map<String, Object> params = dept.getParams();
        QueryWrapper<SysDept> wrapper = Wrappers.query();
        wrapper.eq("del_flag", CommConstants.ONE)
                .eq(ObjectUtil.isNotNull(dept.getId()), "id", dept.getId())
                .like(StringUtils.isNotBlank(dept.getName()), "name", dept.getName())
                .eq(ObjectUtil.isNotNull(dept.getStatus()), "status", dept.getStatus())
                .like(StringUtils.isNotBlank(dept.getLeader()), "leader", dept.getLeader())
                .between(params.get("beginTime") != null && params.get("endTime") != null,
                        "create_time", params.get("beginTime"), params.get("endTime"))
                .orderBy(true, false, "create_time");
        return wrapper;
    }
}
